﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nuzzle.Managers
{
    public static class MapManager
    {
        public static Database.Models.MapModel GetMap(int id)
        {
            if (Database.DatabaseCache.MapCache.FindAll(x => x.ID == id).Count > 0)
            {
                return Database.DatabaseCache.MapCache.FirstOrDefault(x => x.ID == id);
            }
            else
            {
                return null;
            }
        }

        public class Pathfinding
        {
            public List<short> Movements { get; set; }

            public Pathfinding(List<short> movements)
            {
                this.Movements = movements;
            }

            public short Destination
            {
                get
                {
                    return Convert(Movements.LastOrDefault());
                }
            }

            public bool IsEmpty
            {
                get
                {
                    return Movements.Count == 0;
                }
            }

            public short Convert(short cell)
            {
                return (short)(cell & 4095);
            }
        }

        public class MapEngine
        {
            public Database.Models.MapModel Map { get; set; }
            public List<Network.GameClient> Clients = new List<Network.GameClient>();

            public MapEngine(Database.Models.MapModel map)
            {
                this.Map = map;
            }

            public void AddPlayer(Network.GameClient client)
            {
                lock (Clients)
                {
                    this.Send(new Protocol.Messages.Game.Context.Roleplay.GameRolePlayShowActorMessage
                        (client.Character.ToGameRolePlayCharacterInformations()));
                    Clients.Add(client);
                    ShowMap(client);
                }
            }

            public void RemovePlayer(Network.GameClient client)
            {
                lock (Clients)
                {
                    if (Clients.Contains(client))
                    {
                        Clients.Remove(client);
                        this.Send(new Protocol.Messages.Game.Context.GameContextRemoveElementMessage(client.Character.ID));
                    }
                }
            }

            public void Send(Protocol.NetworkMessage message)
            {
                foreach (var client in Clients.ToArray())
                {
                    client.Send(message);
                }
            }

            public void ShowMap(Network.GameClient client)
            {
                client.Send(new Protocol.Messages.Game.Context.Roleplay.CurrentMapMessage(this.Map.ID));
            }

            public void SendComplementaryInformations(Network.GameClient client)
            {
                lock (Clients)
                {
                    var charactersType = new List<Protocol.Types.Game.Context.Roleplay.GameRolePlayCharacterInformations>();
                    foreach (var p in this.Clients)
                    {
                        charactersType.Add(p.Character.ToGameRolePlayCharacterInformations());
                    }
                    client.Send(new Protocol.Messages.Game.Context.Roleplay.MapComplementaryInformationsDataMessage(
                        (short)this.Map.SubareaID, this.Map.ID, 0, charactersType));
                    charactersType.Clear();
                }
            }

            public void SwitchMap(Network.GameClient client, int mapid)
            {
                if (mapid == this.Map.TopMap)
                {
                    client.Teleport(mapid, client.Character.CellID + 532);
                }
                else if (mapid == this.Map.RightMap)
                {
                    client.Teleport(mapid, client.Character.CellID - 13);
                }
                else if (mapid == this.Map.BottomMap)
                {
                    client.Teleport(mapid, client.Character.CellID - 532);
                }
                else if (mapid == this.Map.LeftMap)
                {
                    client.Teleport(mapid, client.Character.CellID + 13);
                }
                else
                {

                }
            }
        }
    }
}
